package com.jushu.storbox.manager;

import java.util.HashMap;
import java.util.Set;

import android.app.Activity;

public class ActivityTaskManager {
	private static ActivityTaskManager activityTaskManager=null;
	private HashMap<String, Activity> activityMap=null;
	private ActivityTaskManager(){
		activityMap=new HashMap<String, Activity>();
	}
	public static synchronized ActivityTaskManager getInstance() {
		if(activityTaskManager==null){
			activityTaskManager=new ActivityTaskManager();
		}
		return activityTaskManager;
	}
	public Activity putActivity(String name,Activity activity){
		return activityMap.put(name, activity);
	}
	/**
	 * @param name
	 * @return
	 */
	public Activity getActivity(String name){
		return activityMap.get(name);
	}
	
	public boolean isEmpty(){
		return activityMap.isEmpty();
	}
	public int size(){
		return activityMap.size();
	}
	/**
	 * @param name 
	 * @return 
	 */
	public boolean containsName(String name){
		return activityMap.containsKey(name);
	}
	/**
	 * @param activity
	 */
	public boolean containsActivity(Activity activity) {
		return  activityMap.containsValue(activity);
	}
	public void closeAllActivity(){
		Set<String> activityNames=activityMap.keySet();
		for (String string : activityNames) {
			finishActivity(activityMap.get(string));
		}
	}
	public void closeAllActivityEnhance(String nameSpecified){
		Set<String> activityNames=activityMap.keySet();
		for (String string : activityNames) {
			if (string.equals(nameSpecified)) {
				continue;
			}
			finishActivity(activityMap.get(string));
		}
	}
	/**
	 * @param nameSpecified
	 */
	public void closeAllActivityExceptOne(String nameSpecified){
		Set<String> activityNames=activityMap.keySet();
		Activity activitySpecified = activityMap.get(nameSpecified);
		for (String string : activityNames) {
			if (string.equals(nameSpecified)) {
				continue;
			}
			finishActivity(activityMap.get(string));
		}
		activityMap.clear();
		activityMap.put(nameSpecified, activitySpecified);
	}
	/**
	 * @param name
	 */
	public void removeActivity(String name){
		Activity activity=activityMap.remove(name);
		finishActivity(activity);
	}
	private void finishActivity(Activity activity) {
		// TODO Auto-generated method stub
		if(activity!=null){
			activity.finish();
		}
	}

}
